package com.zaofan.service;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.Bidi;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;

import com.zaofan.bean.Account;
import com.zaofan.bean.AccountLog;
import com.zaofan.constant.TypeConstant;
import com.zaofan.dao.AccountDao;
import com.zaofan.dao.AccountLogDao;
import com.zaofan.exception.ServiceException;
import com.zaofan.utils.C3p0DataSourceUtils;
import com.zaofan.utils.SqlBuilderUtils;

public class AccountLogService {
	private AccountLogDao accountLogDao = new AccountLogDao();
	private AccountDao accountService = new AccountDao();

	public void delete(AccountLog accountLog) {
		accountLogDao.del(accountLog);
	}

	public void add(AccountLog accountLog) {
		accountLogDao.add(accountLog);
	}

	public void update(AccountLog accountLog) {
		accountLogDao.update(accountLog);
	}

	public List<AccountLog> findAll(Map<String, Object> params) {
		List<AccountLog> data = accountLogDao.findAll(params);
		if (data == null) {
			data = new ArrayList<>();
		}
		return data;
	}

	public Long count(Map<String, Object> params) {
		return accountLogDao.count(params);
	}

	public AccountLog findById(Integer id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		return accountLogDao.findOne(params);
	}

	public void addByAdmin(AccountLog log) throws ServiceException {
		Map<String, Object> params = new HashMap<>();
		params.put("id", log.getAccountId());
		Account account = accountService.findOne(params);
		if (account == null) {
			throw new ServiceException("账户信息错误");
		}
		BigDecimal money = new BigDecimal(0);
		try {
			money = log.getMoney();
		} catch (Exception e) {
			throw new ServiceException("操作余额有误");
		}
		AccountLog data = new AccountLog();
		data.setAccountId(account.getId());
		data.setAccountMobile(account.getMobile());
		data.setAddTime(new Date());
		data.setMoney(money);
		data.setBeforeMoney(account.getMoney());
		data.setAfterMoney(money.add(account.getMoney()));
		if(money.compareTo(new BigDecimal(0)) > 0){
			data.setType(TypeConstant.log_type_admin_add);
		}else{
			data.setType(TypeConstant.log_type_admin_del);
		}
		data.setMark(log.getMark());
		account.setMoney(money.add(account.getMoney()));
		Connection connection = C3p0DataSourceUtils.getConnection();
		try {
			connection.setAutoCommit(false);
			String sql = SqlBuilderUtils.builderInsertSql(AccountLog.class, data);
			QueryRunner runner = new QueryRunner();
			runner.update(connection, sql);
			sql = SqlBuilderUtils.builderUpdateSql(Account.class, account);
			runner.update(connection, sql);
			connection.commit();
		} catch (Exception e) {
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public boolean hasActivityLog() {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("type", TypeConstant.log_type_rechage_return_activity);
		AccountLog log = accountLogDao.findOne(params);
		if(log == null){
			return false;
		}
		return true;
	}



}
